<?php
defined('IN_PHPCMS') or exit('No permission resources.'); 

/* 加载微信官方接口 */
include "wechat.class.php";
$options = array(
	'token'=>'123456', //填写你设定的key
		'debug'=>true,
		'logcallback'=>'logdebug'
);
$weObj = new Wechat($options);
$weObj->valid();
 
if($_GET['shopid']){
	$shopid = intval($_GET['shopid']);
}
 
 
/* 判断类型处理 */
$type = $weObj->getRev()->getRevType();
$db = pc_base::load_model('content_model');
$categorys = getcache('category_content_1','commons');
$model_arr = getcache('model', 'commons'); 
$wxdb = pc_base::load_model('wxuser_model');
$memberdb = pc_base::load_model('member_model');


switch($type) {
	case Wechat::MSGTYPE_TEXT:  //文本类型消息
		//处理关键词自动回复
		$keyword = safe_replace($weObj->getRevContent());
	
 		
		if($keyword=='会员卡' || $keyword=='绑定' || $keyword=='1' ){
			$eventarray = $weObj->getRev()->getRevEvent();
			$wxid = $weObj->getRevFrom();
			$is_wxid =  $wxdb->query('select * from v9_wxuser where wx_id="'.$wxid.'" order by wx_addtime DESC limit 1');
			$is_wxid  =mysql_fetch_assoc($is_wxid);
			if($is_wxid){
				$userinfo = $memberdb->get_one(array('mobile'=>$is_wxid['wx_mobile']));
				$sysmess = "尊敬的（".$is_wxid['wx_mobile']."）客户，您当前账户余额：".$userinfo['amount']."元 , 感谢您的使用 \n<a href='".APP_PATH."index.php?shopid=".$shopid."&wxid=".$weObj->getRevFrom()."'>爱车盟</a>";
			}else{
				$sysmess = "对不起，你还没有绑定爱车盟会员卡。马上点击<a href='".APP_PATH."index.php?m=member&c=mobile&a=login&shopid=".$shopid."&wxid=".$weObj->getRevFrom()."'>绑定手机号</a> 进行绑定啦。";
			}
			$weObj->text($sysmess)->reply();
			exit;
		}elseif($keyword=='优惠' || $keyword=='2' ){
		
			$datas = $db->query('SELECT * FROM phpcms_news WHERE catid=13 ORDER BY listorder DESC,inputtime DESC LIMIT 7' );
			$datas = $db->fetch_array($datas);
			
			$newv = array();
			foreach($datas as $_v) {
				$newv['Title'] = $_v['title'];
				$newv['Description'] = $_v['description'];
				$newv['PicUrl'] = $_v['thumb'];
				$newv['Url'] = APP_PATH."index.php?m=content&c=index&a=show&catid=13&id=".$_v['id']."&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
				$infos[] = $newv;
			}
			$more = array();
			$more[0]['Title'] = '更多内容>>';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = '';
			$more[0]['Url'] = APP_PATH.'index.php?m=content&c=index&a=lists&catid=13&shopid='.$shopid."&wxid=".$weObj->getRevFrom();
			$infos = array_merge_recursive($infos,$more);
			$weObj->news($infos)->reply();
		}elseif($keyword=='附近' || $keyword=='3' ){
			$more = array();
			$more[0]['Title'] = '查看附近的店锖>>';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/lianmeng.png';
			$more[0]['Url'] = APP_PATH."index.php?m=content&c=shop&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
			
		}elseif($keyword=='我的车' || $keyword=='4' ){
		
			$more = array();
			$more[0]['Title'] = '查看我的车辆>>';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/mycar.png';
			$more[0]['Url'] = APP_PATH."index.php?m=member&c=mobile&a=mycar&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
			
		}elseif($keyword=='主页' || $keyword=='首页' || $keyword=='5' ){
		
			$more = array();
			$more[0]['Title'] = '爱车盟首页';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/index.png';
			$more[0]['Url'] = APP_PATH."index.php?shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
		
		}else{
			$memberdb->set_model(13);
			$shopdata = $memberdb->get_one(array('userid'=>$shopid));
			$sysmess = $shopdata['mes_nocon'];
			$weObj->text($sysmess)->reply();
		}
		
		
		break;
	case Wechat::MSGTYPE_EVENT:		//关注时回复
		$eventarray = $weObj->getRev()->getRevEvent();
		 /* $weObj->text(array2string($eventarray))->reply();
		exit;  */
		//处理关键词自动回复
		$keyword = $eventarray['key'];
		

		if($keyword=='1'){
			$eventarray = $weObj->getRev()->getRevEvent();
			$wxid = $weObj->getRevFrom();
			//$is_wxid =  $wxdb->get_one(array('wx_id'=>$wxid));
			$is_wxid =  $wxdb->query('select * from v9_wxuser where wx_id="'.$wxid.'" order by wx_addtime DESC limit 1');
			$is_wxid  =mysql_fetch_assoc($is_wxid);
			if($is_wxid){
				$userinfo = $memberdb->get_one(array('mobile'=>$is_wxid['wx_mobile']));
				$sysmess = "尊敬的（".$is_wxid['wx_mobile']."）客户，您当前账户余额：".$userinfo['amount']."元 , 感谢您的使用 \n<a href='".APP_PATH."index.php?shopid=".$shopid."&wxid=".$weObj->getRevFrom()."'>爱车盟</a>";
			}else{
				$sysmess = "对不起，你还没有绑定爱车盟会员卡。马上点击<a href='".APP_PATH."index.php?m=member&c=mobile&a=login&shopid=".$shopid."&wxid=".$weObj->getRevFrom()."'>绑定手机号</a> 进行绑定啦。";
			}
			$weObj->text($sysmess)->reply();
			exit;
		}elseif($keyword=='2'){
		
			$more = array();
			$more[0]['Title'] = '查看交易明细';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/paylog.png';
			$more[0]['Url'] = APP_PATH."index.php?m=member&c=mobile&a=paylog&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
		
		}elseif($keyword=='3'){
		
			$more = array();
			$more[0]['Title'] = '查看我的消息';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/mymessage.png';
			$more[0]['Url'] = APP_PATH."index.php?m=member&c=mobile&a=mymessage&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
		
		}elseif($keyword=='4'){
		
			$datas = $db->query('SELECT * FROM phpcms_news WHERE catid=13 ORDER BY listorder DESC,inputtime DESC LIMIT 7' );
			$datas = $db->fetch_array($datas);
		
			$newv = array();
			foreach($datas as $_v) {
				$newv['Title'] = $_v['title'];
				$newv['Description'] = $_v['description'];
				$newv['PicUrl'] = $_v['thumb'];
				$newv['Url'] = APP_PATH."index.php?m=content&c=index&a=show&catid=13&id=".$_v['id']."&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
				$infos[] = $newv;
			}
			$more = array();
			$more[0]['Title'] = '更多优惠内容>>';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = '';
			$more[0]['Url'] = APP_PATH.'index.php?m=content&c=index&a=lists&catid=13&shopid='.$shopid."&wxid=".$weObj->getRevFrom();
			$infos = array_merge_recursive($infos,$more);
			$weObj->news($infos)->reply();
			
		}elseif($keyword=='5'){
		
			$more = array();
			$more[0]['Title'] = '查看附近的店锖>>';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/lianmeng.png';
			$more[0]['Url'] = APP_PATH."index.php?m=content&c=shop&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
			
		}elseif($keyword=='9'){
		
			$more = array();
			$more[0]['Title'] = '查看我的车辆>>';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/mycar.png';
			$more[0]['Url'] = APP_PATH."index.php?m=member&c=mobile&a=mycar&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
			
		}elseif($keyword=='10'){
		
			$more = array();
			$more[0]['Title'] = '关于爱车盟';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/about.png';
			$more[0]['Url'] = APP_PATH."index.php?m=content&c=index&a=lists&catid=15&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
		}elseif($keyword=='11'){
		
			$more = array();
			$more[0]['Title'] = '会员中心';
			$more[0]['Description'] = '';
			$more[0]['PicUrl'] = IMG_PATH.'wximg/member.png';
			$more[0]['Url'] = APP_PATH."index.php?m=member&c=mobile&a=mycard&shopid=".$shopid."&wxid=".$weObj->getRevFrom();
			$weObj->news($more)->reply();
			
		}else{
			$memberdb->set_model(13);
			$shopdata = $memberdb->get_one(array('userid'=>$shopid));
			$sysmess = "你好,感谢您的关注!";
			$sysmess = $shopdata['mes_guanzhu'];
			$weObj->text($sysmess)->reply();
			
		}
		exit;
		break;
	case Wechat::MSGTYPE_IMAGE:
		$weObj->text("抱歉,我暂时看不懂图片信息,不会对图片信息进任何处理..您可以文字描述方式试试")->reply();
		exit;
		break;
	case Wechat::MSGTYPE_LOCATION:
		//$weObj->text("抱歉,我暂时看不懂图片信息,不会对图片信息进任何处理..您可以文字描述方式试试")->reply();
		exit;
		break;
	case Wechat::MSGTYPE_VOICE:
		$weObj->text("抱歉,我暂时不懂语音信息,.您可以文字描述方式试试")->reply();
		exit;
		break;
	default:
		$weObj->text("help info")->reply();
		exit;
}



function logdebug($text){
	file_put_contents('log.txt',$text."\n",FILE_APPEND);		
};


function hitcon($id){
		$modelid = 21;
		$hitdb = pc_base::load_model('hits_member_model');
		$r1 = $hitdb->get_one(array('hitsid'=>$id,'modelid'=>$modelid));
		if(!$r1) return false;
		$views = $r1['views'] + 1;
		$yesterdayviews = (date('Ymd', $r1['updatetime']) == date('Ymd', strtotime('-1 day'))) ? $r1['dayviews'] : $r1['yesterdayviews'];
		$dayviews = (date('Ymd', $r1['updatetime']) == date('Ymd', SYS_TIME)) ? ($r1['dayviews'] + 1) : 1;
		$weekviews = (date('YW', $r1['updatetime']) == date('YW', SYS_TIME)) ? ($r1['weekviews'] + 1) : 1;
		$monthviews = (date('Ym', $r1['updatetime']) == date('Ym', SYS_TIME)) ? ($r1['monthviews'] + 1) : 1;
		$sql = array('views'=>$views,'yesterdayviews'=>$yesterdayviews,'dayviews'=>$dayviews,'weekviews'=>$weekviews,'monthviews'=>$monthviews,'updatetime'=>SYS_TIME);
		return $hitdb->update($sql, array('hitsid'=>$id,'modelid'=>$modelid));
}


/* 地图坐标处理 */
     function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2) 
    { 
		
        $EARTH_RADIUS=6378.137; 
        $PI=3.1415926; 
        $radLat1 = $lat1 * $PI / 180.0; 
        $radLat2 = $lat2 * $PI / 180.0; 
        $a = $radLat1 - $radLat2; 
        $b = ($lng1 * $PI / 180.0) - ($lng2 * $PI / 180.0); 
        $s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2))); 
        $s = $s * $EARTH_RADIUS; 
        $s = round($s * 1000); 
        if ($len_type > 1) 
        { 
         $s /= 1000; 
        } 
        return round($s,$decimal); 
    }

	function mapBar2WGS84($x,$y){
		$data = file_get_contents("http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=".$y."&y=".$x);
		$data =  json_decode($data,true);
		$data['x'] =  base64_decode($data['x']);
		$data['y'] =  base64_decode($data['y']);
		return $data;
	}


?>